package com.platform.controller;

import com.platform.annotation.SysOptionLog;
import com.platform.entity.OrderDetailEntity;
import com.platform.entity.OrderEntity;
import com.platform.entity.excel.OrderExcel;
import com.platform.service.OrderDetailService;
import com.platform.service.OrderService;
import com.platform.service.UserService;
import com.platform.util.OrderStatusEnum;
import com.platform.util.OrderTypeEnum;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;
import com.platform.utils.excel.ExcelExport;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**订单管理:SPC兑换订单 及 订单管理:奖金兑换订单
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-13 10:41:09
 */
@RestController
@RequestMapping("orderspc")
public class OrderSpcController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("orderspc:list")
    public R list(@RequestParam Map<String, Object> params) {
        // 查询列表数据
        Query query = new Query(params);

        List<OrderEntity> orderList = orderService.querySpcList(query);
        int total = orderService.querySpcTotal(query);

        PageUtils pageUtil = new PageUtils(orderList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("orderspc:info")
    public R info(@PathVariable("id") Integer id) {
        OrderEntity order = orderService.querySpcObject(id);
        String orderNo = order.getOrderNo();
        Integer productId = order.getProductId();
        if(productId == null){
            OrderDetailEntity orderDetailEntity = orderDetailService.queryObject(orderNo);
            order.setProductId(orderDetailEntity.getProId());
            order.setProductName(orderDetailEntity.getProName());
        }
        return R.ok().put("orderspc", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @SysOptionLog("添加Spc兑换订单")
    @RequiresPermissions("orderspc:save")
    public R save_orderSpc(@RequestBody OrderEntity order) {
        orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @SysOptionLog("修改Spc兑换订单")
    @RequiresPermissions("orderspc:update")
    public R update_orderSpc(@RequestBody OrderEntity order) {
        orderService.update(order);


        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("orderspc:delete")
    public R delete_orderSpc(@RequestBody Integer[] ids) {
        orderService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<OrderEntity> list = orderService.queryList(params);

        return R.ok().put("list", list);
    }

    /**
     * 总计
     */
    @RequestMapping("/queryTotal")
    public R queryTotal(@RequestParam Map<String, Object> params) {
        int sum = orderService.queryTotal(params);

        return R.ok().put("sum", sum);
    }

    /**
     * 确定收货
     *
     * @param rowId
     * @return
     */
    @RequestMapping("/confirm")
    @RequiresPermissions("orderspc:confirm")
    public R confirm(@RequestBody Integer rowId) {
        orderService.confirmSpc(rowId);

        return R.ok();
    }
//    @RequestMapping("/send")
//    @RequiresPermissions("orderspc:send")
//    public R reject(@RequestBody Integer rowId) {
//        orderService.rejectSpc(rowId);
//
//        return R.ok();
//    }

    /**
     * 发货
     *
     * @param order
     * @return
     */
    @RequestMapping("/send")
    @RequiresPermissions("orderspc:send")
    public R sendGoods(@RequestBody OrderEntity order) {
        orderService.sendGoods(order);

        return R.ok();
    }
    @RequestMapping("/export")
    @RequiresPermissions("orderspc:export")
    public void code(
            @RequestParam(value = "orderNo", required = false) String orderNo,
            @RequestParam(value = "userPhone", required = false) String userPhone,
            @RequestParam(value = "payStatus", required = false) Integer payStatus,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "type", required = false) Integer type,
            @RequestParam(value = "index", required = false) Integer index,
            @RequestParam(value = "size", required = false) Integer size,
            @RequestParam(value = "payWay", required = false) Integer payWay,
            @RequestParam(value = "merchantId", required = false) Integer merchantId
            , HttpServletResponse response) throws IOException {
        List<OrderExcel> orderList = orderService.queryListExcel(orderNo, userPhone, payStatus, status, index, size, type,payWay,merchantId);

        ExcelExport ee = new ExcelExport("SPC兑换订单");
        String[] header = new String[]{"订单号", "用户编号", "手机号", "产品名称", "产品款式","数量", "订单金额", "实付金额", "收货人", "收货人手机", "收货地址", "订单状态", "付款状态", "支付方式", "支付时间"};
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();


        for (OrderExcel orderExcel : orderList) {
            LinkedHashMap<String, Object> map = new LinkedHashMap<>();
            String orderno = orderExcel.getOrderNo();
            String productname = orderExcel.getProductName();
            map.put("orderNo", orderExcel.getOrderNo());
            map.put("userId", orderExcel.getUserId());
            map.put("userPhone", orderExcel.getUserPhone());

            if(StringUtils.isNotBlank(productname)){
                //老版本
                map.put("productName", orderExcel.getProductName());
                map.put("proStyle","无");
                map.put("number", orderExcel.getNumber());
                map.put("amount", orderExcel.getAmount());
                map.put("realAmount", orderExcel.getRealAmount());
            }else {
                //新版本
                OrderDetailEntity orderDetailEntity = orderDetailService.queryObject(orderno);
                if(orderDetailEntity == null ){
                    map.put("error", "此订单没有详情");
                    list.add(map);
                    continue;
                }
                map.put("productName", orderDetailEntity.getProName());
                map.put("proStyle",orderDetailEntity.getStyleName());
                map.put("number", orderDetailEntity.getNum());
                map.put("amount", orderExcel.getAmount());
                map.put("realAmount", orderDetailEntity.getPrice());
            }
            map.put("receiveUser", orderExcel.getReceiveUser());
            map.put("receivePhone", orderExcel.getReceivePhone());
            map.put("address",orderExcel.getProvince()+orderExcel.getCity()+orderExcel.getArea()+ orderExcel.getAddress());
            map.put("status", OrderStatusEnum.getDesc(orderExcel.getStatus()) );
            map.put("payStatus", orderExcel.getPayStatus()==2?"已支付":"未支付");
            map.put("payWay", OrderTypeEnum.getDesc(orderExcel.getPayWay()));
            map.put("payDate", orderExcel.getCreateDate());
            list.add(map);
        }
        ee.addSheetByMap("SPC兑换订单", list, header);
        ee.export(response);
    }
}
